package com.cmccsi.service.impl;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;

import com.cmccsi.entity.qxuserDto;
import com.cmccsi.mapper.qxuserDtoMapper;
import com.cmccsi.service.QxUserService;
import com.cmccsi.utils.MybatisUtil;

public class QxUserServiceImpl implements QxUserService {

	private SqlSessionFactory sessionFactory = MybatisUtil.getInstance();
	private QxUserRedisServiceImpl userRedis = new QxUserRedisServiceImpl();

	private Logger loger = Logger.getLogger(DomainServiceImpl.class);

	@Override
	public Boolean save(qxuserDto user, int optType) {
		SqlSession session = sessionFactory.openSession();
		try {
			Long userid = user.getId();
			if (userid == null) {
				user.setId((long) (0));
			}
			qxuserDtoMapper userMapper = session
					.getMapper(qxuserDtoMapper.class);
			qxuserDto usrHaved = userMapper
					.selectByModileOrUsernameAndDomainId(user.getDomainid(),
							user.getUserName(), user.getMobilePhone());
			if (usrHaved == null || usrHaved.getId() <= 0) {
				usrHaved = userMapper.selectByPrimaryKey(user.getId());
			}

			if (usrHaved == null || usrHaved.getId() <= 0) {
				userMapper.insert(user);
				session.commit();
				userRedis.save(user, 0);
				return true;
			} else {
				usrHaved.setDomainid(user.getDomainid());
				usrHaved.setUserName(user.getUserName());
				usrHaved.setMobilePhone(user.getMobilePhone());
				usrHaved.setCyloginflag(user.getCyloginflag());
				return update(usrHaved);
			}

		} catch (Exception e) {
			e.printStackTrace();

			loger.error(e.getMessage(), e);
			loger.error("出错的User的username：" + user.getDisplayName());
		} finally {
			session.close();
		}
		return false;
	}

	@Override
	public Boolean update(qxuserDto user) {
		SqlSession session = sessionFactory.openSession();
		try {
			qxuserDtoMapper userMapper = session
					.getMapper(qxuserDtoMapper.class);
			userMapper.updateByPrimaryKey(user);
			session.commit();
			userRedis.update(user);
			return true;
		} catch (Exception e) {
			e.printStackTrace();

			loger.error(e.getMessage(), e);
			loger.error("出错的User的username：" + user.getDisplayName());
		} finally {
			session.close();
		}
		return false;
	}

	@Override
	public qxuserDto find(Long id) {
		qxuserDto user = null;
		SqlSession session = sessionFactory.openSession();
		try {
			qxuserDtoMapper userMapper = session
					.getMapper(qxuserDtoMapper.class);
			user = userMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();

			loger.error(e.getMessage(), e);
		} finally {
			session.close();
		}
		return user;
	}

	@Override
	public Boolean delete(Long id) {
		SqlSession session = sessionFactory.openSession();
		try {
			qxuserDtoMapper userMapper = session
					.getMapper(qxuserDtoMapper.class);
			userMapper.deleteByPrimaryKey(id);
			session.commit();
			userRedis.delete(id);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			loger.error(e.getMessage(), e);
		} finally {
			session.close();
		}
		return false;
	}

	@Override
	public Boolean deleteByMobile(String Mobile) {
		SqlSession session = sessionFactory.openSession();
		try {
			qxuserDtoMapper userMapper = session
					.getMapper(qxuserDtoMapper.class);
			qxuserDto user = userMapper.selectByModile(Mobile);
			if (user != null) {
				return delete(user.getId());
			}
		} catch (Exception e) {
			e.printStackTrace();

			loger.error(e.getMessage(), e);
		} finally {
			session.close();
		}

		return false;
	}

	@Override
	public Boolean deleteByDomainId(String DomainId) {
		SqlSession session = sessionFactory.openSession();
		try {
			qxuserDtoMapper userMapper = session
					.getMapper(qxuserDtoMapper.class);
			userMapper.deleteByDomainId(DomainId);
			session.commit();
			// 鏆備笉澶勭悊娓呯┖鎵�湁閮ㄩ棬浜哄憳鐨剅edis鏁版嵁缂撳瓨鐨勭姸鎬�
			return true;
		} catch (Exception e) {
			e.printStackTrace();

			loger.error(e.getMessage(), e);
		} finally {
			session.close();
		}
		return false;
	}
}
